﻿using UnityEditor;
using UnityEngine;

namespace Core.Editor
{
    /// <summary>
    /// 获取资源路径相关的实用函数。
    /// </summary>
    public static class AssetLocation
    {
        [MenuItem("Assets/Debug Asset Path", priority = 1000)]
        private static void DebugAssetPath()
        {
            if (!TryGetSelectedObject(out var selObj)) return;

            var assetPath = AssetDatabase.GetAssetPath(selObj);
            EditorGUIUtility.systemCopyBuffer = assetPath;
            Debug.Log($"Asset path is {assetPath}");
        }

        [MenuItem("Assets/Debug Addressable Path", priority = 1001)]
        private static void DebugAddressablePath()
        {
            if (!TryGetSelectedObject(out var selObj)) return;

            var assetPath = AssetDatabase.GetAssetPath(selObj);
            var addressableName = GetAddressableName(assetPath);
            EditorGUIUtility.systemCopyBuffer = addressableName;
            Debug.Log($"Addressable path is {addressableName}");
        }

        private static string GetAddressableName(string assetPath)
        {
            // 使用更安全的路径处理方法
            var fileName = System.IO.Path.GetFileNameWithoutExtension(assetPath);
            return System.IO.Path.GetFileName(fileName);
        }

        private static bool TryGetSelectedObject(out UnityEngine.Object selObj)
        {
            selObj = Selection.activeObject;
            if (selObj != null) return true;

            Debug.LogWarning("No object selected");
            return false;
        }
    }
}